package com.t282.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.t282.crm.entity.CstActivity;
import com.t282.crm.service.impl.CstActivityServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "客户管理")
@RequestMapping("/api/activity")
public class ActivityController {
    @Autowired
    private CstActivityServiceImpl cstActivityService;

    @ApiOperation(value = "查询交往记录")
    @RequestMapping("findContactRecords")
    public String findContactRecords(String custName,String custNo, HttpServletRequest request){

        QueryWrapper<CstActivity> queryWrapper = new QueryWrapper<CstActivity>();
        queryWrapper.eq("atv_cust_no",custNo);

        List<CstActivity> cstActivityList = cstActivityService.list(queryWrapper);
        request.setAttribute("cstActivityList",cstActivityList);
        request.setAttribute("custName",custName);
        request.setAttribute("custNo",custNo);
        return "activity/list";
    }

    @ApiOperation(value = "查询交往记录")
    @RequestMapping("getContactRecords")
    public String getContactRecords(Long atvId,String custName,HttpServletRequest request){
        CstActivity activity = cstActivityService.getById(atvId);
        request.setAttribute("activity",activity);
        request.setAttribute("custName",custName);
        return "activity/edit";
    }

    @ApiOperation(value = "编辑交往记录")
    @RequestMapping("updateContactRecords")
    public String updateContactRecords(String date,CstActivity cstActivity) throws UnsupportedEncodingException, ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date datetime = simpleDateFormat.parse(date);
        cstActivity.setAtvDate(datetime);
        boolean flag = cstActivityService.updateById(cstActivity);
        if(flag){
            return "redirect:/api/activity/findContactRecords?custName="+ URLEncoder.encode(cstActivity.getAtvCustName(),"UTF-8")+ "&custNo="+cstActivity.getAtvCustNo();
        }
        return "redirect:/api/activity/getContactRecords?custName="+ URLEncoder.encode(cstActivity.getAtvCustName(),"UTF-8")+"&atvId="+ cstActivity.getAtvId();
    }

    @ApiOperation(value = "删除交往记录")
    @RequestMapping("deleteContactRecords/{atvId}")
    @ResponseBody
    public Map deleteContactRecords(@PathVariable Long atvId){
        Map map = new HashMap();
        boolean result = cstActivityService.removeById(atvId);
        map.put("result",result);
        return map;
    }

    //跳往新增
    @ApiOperation(value = "新建交往记录")
    @RequestMapping("addContactRecords")
    public String addContactRecords(String atvCustNo,String atvCustName,HttpServletRequest request){
        request.setAttribute("atvCustNo",atvCustNo);
        request.setAttribute("atvCustName",atvCustName);
        return "activity/add";
    }

    @ApiOperation(value = "新建交往记录")
    @RequestMapping("insertContactRecords")
    public String insertContactRecords(String date,CstActivity cstActivity) throws ParseException, UnsupportedEncodingException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        Date datetime = simpleDateFormat.parse(date);
        cstActivity.setAtvDate(datetime);
        boolean result = cstActivityService.save(cstActivity);
        if(result){
            return "redirect:/api/activity/findContactRecords?custName="+ URLEncoder.encode(cstActivity.getAtvCustName(),"UTF-8")+ "&custNo="+cstActivity.getAtvCustNo();
        }
        return "redirect:/api/activity/addContactRecords?atvCustName="+ URLEncoder.encode(cstActivity.getAtvCustName(),"UTF-8")+ "&atvCustNo="+cstActivity.getAtvCustNo();
    }
}
